package com.example.a28796.beautiful;

import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a28796.beautiful.styAdapter.HuodongAdapter;
import com.example.a28796.beautiful.stybean.HuodongBean;
import com.example.a28796.beautiful.stynet.NetConfig;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HuodongActivity extends AppCompatActivity {
    private RecyclerView mRecyclerGrid;
    private StaggeredGridLayoutManager mStaggeredGridLayoutManager;
    private HuodongAdapter mAdapter;
    private List<HuodongBean.ObjBean.ListDataBean> mData;
    private List<Integer> mHeight;
    private Random random;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private int currentPage = 1;
    private GridLayoutManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huodong);
        initView();
        initData();
        setData();
    }

    private void setData() {
        mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerGrid.setLayoutManager(mStaggeredGridLayoutManager);
        mRecyclerGrid.setAdapter(mAdapter);

        /*mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                loadMore(1);
            }
        });
        mRecyclerGrid.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState==RecyclerView.SCROLL_STATE_IDLE){
                    int last =  mStaggeredGridLayoutManager.findLastVisibleItemPositions();
                    if(last==mData.size()-1){
                        loadMore(currentPage);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });*/
    }

    private void initData() {
        mData = new ArrayList<>();
        mHeight = new ArrayList<>();
        random = new Random();
        loadMore(1);

    }
    private void loadMore(final int position){
        //String path = String.format(NetConfig.HUODONG_URL,currentPage);
        x.http().get(new RequestParams(NetConfig.HUODONG_URL), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                HuodongBean huodongBean = new Gson().fromJson(result, HuodongBean.class);
               //currentPage++;
               /* if(position==1){
                    mData.clear();
                }*/
                mData.addAll(huodongBean.getObj().getListData());
                for (int i = 0; i < mData.size(); i++) {
                    int ran = random.nextInt(30);
                    mHeight.add(50+ran);
                }
                mAdapter.notifyDataSetChanged();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        mAdapter = new HuodongAdapter(this,mData,mHeight);
        mAdapter.setOnItemClickLitener(new HuodongAdapter.OnItemClickLitener()
        {

            @Override
            public void onItemClick(View view, int position)
            {
                /*Toast.makeText(HuodongActivity.this, position + " click",
                        Toast.LENGTH_SHORT).show();*/
            }

            @Override
            public void onItemLongClick(View view, final int position)
            {
                /*Toast.makeText(HuodongActivity.this, position + " long click",
                        Toast.LENGTH_SHORT).show();*/
                View dialogView = getLayoutInflater().inflate(R.layout.dialog, null);
                final AlertDialog.Builder dialog = new AlertDialog.Builder(HuodongActivity.this, R.style.dialog);
                TextView tvok = (TextView) dialogView.findViewById(R.id.queren);
                TextView tvno = (TextView) dialogView.findViewById(R.id.quxiao);
                dialog.setView(dialogView);
                dialog.create();
                dialog.show();

                tvno.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });
                tvok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mData.remove(position);
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }
    private void initView() {
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.sl_huodong_refresh);
        mRecyclerGrid = (RecyclerView) findViewById(R.id.rv_huodong_show);
    }
}
